package ink.yuanchu.picachoframework.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * 数据表数据初始化
 * @author ziyuan
 * @Date: 2021/04/01/20:29
 * @Description:
 */
public class DBUtils {

    private static final Logger log = LoggerFactory.getLogger(DBUtils.class);

    private static DBUtils dbUtils = new DBUtils();

    private String driver;
    private String url;
    private String username;
    private String password;
    private DriverManagerDataSource dataSource = new DriverManagerDataSource();
    private JdbcTemplate jdbcTemplate= new JdbcTemplate();

    private DBUtils(){
    }

    public static DBUtils getObject(){
        return dbUtils;
    }

    /**
     * 执行脚本
     */
    public void executeScript(){
        String sql = "";
        try {
            log.info("开始执行脚本!");
            InputStream sqlInput = this.getClass().getClassLoader().getResourceAsStream("sqlScript/basic_framework.sql");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(sqlInput));


            int userCount = jdbcTemplate.queryForObject("select count(*) from user;", new Object[]{}, Integer.class);
            int roleCount = jdbcTemplate.queryForObject("select count(*) from role;", new Object[]{}, Integer.class);
            int permissionsCount = jdbcTemplate.queryForObject("select count(*) from permissions;", new Object[]{}, Integer.class);
            int roleAssociatedPermissionsCount = jdbcTemplate.queryForObject("select count(*) from role_associated_permissions;", new Object[]{}, Integer.class);
            int timedTaskCount = jdbcTemplate.queryForObject("select count(*) from timed_task;", new Object[]{}, Integer.class);

            if (timedTaskCount == 0 &&
                    userCount == 0 &&
                    roleCount == 0 &&
                    permissionsCount == 0 &&
                    roleAssociatedPermissionsCount == 0
            ) {
                while ((sql = bufferedReader.readLine()) != null) {
                    if (sql != null && !sql.equals("")) {
                        jdbcTemplate.execute(sql);
                        sql = null;
                    }
                }
            }else {
                log.info("数据表已存在数据，退出执行脚本!");
                return;
            }
            log.info("脚本执行完毕!");
        } catch (IOException e) {
            log.error("error:"+sql);
            log.error("脚本执行错误,已退出!");
            e.printStackTrace();
        }
    }

    /**
     * 数据初始化
     */
    public boolean initializationData(){
        try {
            log.info("开始数据初始化!");
            Properties props = new Properties();
            InputStream input= this.getClass().getClassLoader().getResourceAsStream("initInfo.properties");
            props.load(input);
            this.url = props.getProperty("url");
            this.driver = props.getProperty("driver");
            this.username = props.getProperty("username");
            this.password = props.getProperty("password");

            this.dataSource.setDriverClassName(this.driver);
            this.dataSource.setUrl(this.url);
            this.dataSource.setUsername(this.username);
            this.dataSource.setPassword(this.password);

            this.jdbcTemplate.setDataSource(this.dataSource);
            log.info("数据初始化完毕!");
            return true;
        } catch (Exception e){
            log.error("数据初始化失败!");
            e.printStackTrace();
            return false;
        }
    }
}
